﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SubSonic;
using ExtAspNet;


namespace AppBox
{
    public partial class _default : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }

            //try
            //{
            //    int j = Convert.ToInt32("0");
            //    int i = 1 / j;
            //}
            //catch (Exception ex)
            //{
            //    logger.Error("这是个测试的日志", ex);
            //}
        }

        private void LoadData()
        {
            // tbxUserName.Focus();
        }

        #endregion

        #region Events

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string userName = tbxUserName.Text.Trim();
            string password = tbxPassword.Text.Trim();

            XUser user = new Select().From<XUser>()
                .Where(XUser.NameColumn).IsEqualTo(userName)
                .ExecuteSingle<XUser>();

            if (user != null)
            {
                if (PasswordUtil.ComparePasswords(user.Password, password))
                {
                    if (!user.Enabled)
                    {
                        ExtAspNet.Alert.Show("用户未启用，请联系管理员！");
                    }
                    else
                    {
                        // 登录成功
                        LoginOnlineUser(user);

                        logger.Info(String.Format("登录成功：用户“{0}”", user.Name));
                        FormsAuthentication.RedirectFromLoginPage(userName, true);
                        return;
                    }
                }
                else
                {
                    logger.Warn(String.Format("登录失败：用户“{0}”密码错误", userName));
                    ExtAspNet.Alert.Show("用户名或密码错误！");
                    return;
                }

            }
            else
            {
                logger.Warn(String.Format("登录失败：用户“{0}”不存在", userName));
                Alert.Show("用户名或密码错误！");
                return;
            }
        }




        #endregion
    }
}
